from enum import Enum, IntEnum
from tortoise.models import Model
from tortoise import fields


class RoleENUM(Enum):
    ADMIN = 'ADMIN'
    COACH = 'COACH'
    STUDENT = 'STUDENT'


class GenderENUM(Enum):
    MALE = 'MALE'
    FEMALE = 'FEMALE'
    OTHER = 'OTHER'


class Student_Training_Status(Enum):
    SCHEDULED = 'SCHEDULED'
    COMPLETED = 'COMPLETED'
    CANCELED = 'CANCELED'
    MISSED = 'MISSED'


class Appointments_Status(Enum):
    PENDING = 'PENDING'
    CONFIRMED = 'CONFIRMED'
    COMPLETED = 'COMPLETED'
    CANCELLED = 'CANCELLED'


class Rating(IntEnum):
    ONE = 1
    TWO = 2
    THREE = 3
    FOUR = 4
    FIVE = 5


# class Keys(Model):
#     id = fields.IntField(pk=True, auto_increment=True)
#     name = fields.CharField(max_length=255)
#     private_key = fields.TextField()
#     public_key = fields.TextField()
#     created_at = fields.CharField(max_length=255)


# 用户基础表(所有用户类型的公共信息)
class User(Model):
    user_id = fields.IntField(pk=True, auto_increment=True, description='用户唯一标识')
    username = fields.CharField(max_length=50, null=False, unique=True, description='登录用户名')
    password = fields.CharField(max_length=255, null=False, description='登录密码')
    email = fields.CharField(max_length=50, null=True, description='邮箱')
    phone = fields.CharField(max_length=50, null=False, description='手机号码')
    role = fields.CharEnumField(enum_type=RoleENUM, null=False, description='用户角色')
    avatar_url = fields.CharField(max_length=255, null=True, description='用户头像')
    create_at = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_at = fields.DatetimeField(auto_now=True, description='更新时间')
    is_active = fields.BooleanField(default=True, description='用户是否激活')


# 用户详细信息表
class User_Profiles(Model):
    profile_id = fields.IntField(pk=True, auto_increment=True, description='用户详细信息唯一标识')
    user_id = fields.ForeignKeyField('models.User', related_name='profiles', unique=True, null=False, index=True,
                                     description='用户ID')  # 唯一外键关联
    fullname = fields.CharField(max_length=50, null=False, description='用户姓名')
    gender = fields.CharEnumField(enum_type=GenderENUM, null=False, description='用户性别')
    birthday = fields.DateField(description='用户生日', null=True)
    id_number = fields.CharField(max_length=50, null=False, description='用户身份证号')
    address = fields.CharField(max_length=255, null=True, description='用户地址')


# 教练表
class Coach(Model):
    coach_id = fields.IntField(pk=True, auto_increment=True, description='教练id')
    user_id = fields.ForeignKeyField('models.User', related_name='coach', unique=True, null=False, index=True,
                                     description='用户ID')  # 外键关联
    license_number = fields.CharField(max_length=50, null=False, description='教练证件号码')
    hire_date = fields.DateField(null=False, description='入职日期')
    years_of_experience = fields.IntField(null=False, description='工作年限')
    specialization = fields.CharField(max_length=255, null=True, description='技能专长')
    rating = fields.IntEnumField(enum_type=Rating, default=5, description='评价')
    is_active = fields.BooleanField(default=True, description='是否在职')


# 教练排班表
class Coach_Schedules(Model):
    schedule_id = fields.IntField(pk=True, auto_increment=True, description='排班ID')
    coach_id = fields.ForeignKeyField('models.Coach', related_name='coach_schedules', index=True,
                                      description='教练ID')  # 外键关联
    day_of_week = fields.CharField(max_length=20, null=False, description='星期几,1-7')
    start_time = fields.TimeField(null=False, description='开始时间')
    end_time = fields.TimeField(null=False, description='结束时间')
    is_active = fields.BooleanField(default=True, description='是否可预约')


# 学生信息表
class Student(Model):
    student_id = fields.IntField(pk=True, auto_increment=True, description='学生ID')
    user_id = fields.ForeignKeyField('models.User', related_name='students', unique=True, null=False, index=True,
                                     description='用户ID')  # 外键关联
    enrollment_date = fields.DateField(null=False, description='入学日期')
    current_level = fields.CharField(null=False, max_length=20, description='当前学习阶段')
    license_type = fields.CharField(null=False, max_length=20, description='驾照类型')
    progress_percentage = fields.FloatField(default=0.0, description='学习进度百分比')
    is_active = fields.BooleanField(default=True, description='是否可学')


# 学生训练记录表
class Student_trainings(Model):
    training_id = fields.IntField(pk=True, auto_increment=True, description='训练记录ID')
    student_id = fields.ForeignKeyField('models.Student', related_name='student_trainings', index=True,
                                        description='学生ID')  # 外键关联
    coach_id = fields.ForeignKeyField('models.Coach', related_name='coach_trainings', index=True,
                                      description='教练ID')  # 外键关联
    program_id = fields.ForeignKeyField('models.Training_Programs', related_name='program_trainings', index=True,
                                        description='课程ID')  # 外键关联
    start_time = fields.DatetimeField(null=False, description='开始时间')
    end_time = fields.DatetimeField(null=False, description='结束时间')
    status = fields.CharEnumField(enum_type=Student_Training_Status, default='SCHEDULED',
                                  null=False, description='训练状态')
    feedback = fields.TextField(null=True, description='教练对学生评价')
    rating = fields.IntEnumField(enum_type=Rating, default=5, description='学生对教练评分')
    create_at = fields.DatetimeField(auto_now_add=True, description='创建时间')
    content = fields.TextField(null=False, description='反馈内容')
    response = fields.TextField(null=True, description='教练/管理员回复内容')

    class Meta:
        # 添加唯一约束，确保每个学生对每种program只能有一条记录
        unique_together = ('student_id', 'program_id')


# 学生预约表
class Appointments(Model):
    appointment_id = fields.IntField(pk=True, auto_increment=True, description='预约编号')
    student_id = fields.ForeignKeyField('models.Student', related_name='appointments', index=True, description='学生ID')
    coach_id = fields.ForeignKeyField('models.Coach', related_name='appointments', index=True, description='教练ID')
    program_id = fields.ForeignKeyField('models.Training_Programs', related_name='appointments', index=True,
                                        description='课程ID')
    appointment_date = fields.DateField(description='预约日期')
    start_time = fields.TimeField(description='开始时间')
    end_time = fields.TimeField(description='结束时间')
    status = fields.CharEnumField(enum_type=Appointments_Status, default='PENDING', null=False,
                                  description='预约状态')
    create_at = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_at = fields.DatetimeField(auto_now=True, description='更新时间')


# 管理员训练类别表
class Training_Categories(Model):
    category_id = fields.IntField(pk=True, auto_increment=True, description='训练类别唯一标识')
    name = fields.CharField(max_length=50, null=False, description='训练类别名称')
    description = fields.TextField(null=True, description='训练类别描述')
    create_by = fields.ForeignKeyField('models.User', related_name='created_categories', index=True,
                                       description='创建人')  # 外键关联
    parent_category_id = fields.ForeignKeyField(
        'models.Training_Categories',
        related_name='children_category',
        index=True,
        description='父级训练类别ID',
        null=True
    )
    create_at = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_at = fields.DatetimeField(auto_now=True, description='更新时间')
    is_active = fields.BooleanField(default=True, description='训练类别是否激活')


# 管理员训练项目表
class Training_Programs(Model):
    program_id = fields.IntField(pk=True, auto_increment=True, description='训练项目唯一标识')
    name = fields.CharField(max_length=50, null=False, description='训练项目名称')
    category_id = fields.ForeignKeyField('models.Training_Categories', related_name='programs', index=True,
                                         description='训练类别ID')  # 外键关联
    description = fields.TextField(null=True, description='训练项目描述')
    create_by = fields.ForeignKeyField('models.User', related_name='created_programs', index=True,
                                       description='创建人')  # 外键关联
    create_at = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_at = fields.DatetimeField(auto_now=True, description='更新时间')
    is_active = fields.BooleanField(default=True, description='训练项目是否激活')


# 管理员公告表
class Announcements(Model):
    announcement_id = fields.IntField(pk=True, auto_increment=True, description='公告ID')
    title = fields.CharField(null=False, max_length=255, description='公告标题')
    content = fields.TextField(null=False, description='公告内容')
    created_by = fields.ForeignKeyField('models.User', related_name='announcements', index=True, description='创建者ID')
    create_at = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_at = fields.DatetimeField(auto_now=True, description='更新时间')
    is_active = fields.BooleanField(default=True, description='公告是否激活')
    is_pinned = fields.BooleanField(default=False, description='公告是否置顶')


class AIConversation(Model):
    """
    AI问答记录表
    用于存储用户与AI助手的对话记录
    """
    id = fields.IntField(pk=True, description="主键ID")
    question = fields.TextField(description="用户问题")
    answer = fields.TextField(description="AI回答")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    user = fields.ForeignKeyField('models.User', related_name='ai_conversations',
                                  null=True, description="用户ID（可选）")

    class Meta:
        table = "ai_conversations"
        table_description = "AI问答记录表"

    def __str__(self):
        return f"AIConversation(id={self.id}, question={self.question[:20]}...)"
